package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.BarChartData;
import com.itheima.common.PageBean;
import com.itheima.dtos.PageCustomerDto;
import com.itheima.mapper.CustomerMapper;
import com.itheima.pojo.Customer;
import com.itheima.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    //分页查

    /**
     * 条件分页查
     * @param pageCustomerDto 条件
     * @return 查到的数据
     */
    @Override
    public PageBean<Customer> queryPage(PageCustomerDto pageCustomerDto) {
        PageHelper.startPage(pageCustomerDto.getPage(), pageCustomerDto.getSize());//开启分页

         List<Customer> listCustomer = customerMapper.queryPage(pageCustomerDto);

        Page<Customer> p = (Page<Customer>) listCustomer;
        return new PageBean<Customer>(p.getTotal(), p.getResult());
    }

    //会员年龄统计

    /**
     * 根据年龄统计人数
     * @return 查到的数据
     */
    @Override
    public BarChartData queryCountGroupByAge() {
        List<Map<String, Object>> listMaps = customerMapper.queryCountGroupByAge();
        System.out.println("获取出来的数据:" + listMaps);
        List<Object> ageGroupList = listMaps.stream().map(m -> m.get("group_name")).collect(Collectors.toList());
        List<Object> ageCountList = listMaps.stream().map(m -> m.get("count")).collect(Collectors.toList());

        return new BarChartData(ageGroupList, ageCountList);
    }
}
